package com.sixth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.sixth.annotation.PageInfoAdvice;
import com.sixth.bean.MarketBrand;
import com.sixth.bean.MarketBrandExample;
import com.sixth.bean.common.BasePageInfo;
import com.sixth.bean.common.BasePageInfoVo;
import com.sixth.bean.vo.AdminBrandCreateVo;
import com.sixth.bean.vo.MarketBrandListVO;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketBrandMapper;
import com.sixth.service.AdminBrandService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminBrandServiceImpl
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/8 9:09
 **/
@Transactional
@Service
public class AdminBrandServiceImpl implements AdminBrandService {

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @PageInfoAdvice
    @Override
    public Object list(BasePageInfo basePageInfo, Integer id, String name) {
        if (!StringUtil.isEmpty(name)) {
            name = name.toUpperCase().trim();
        }

        String orderBy = basePageInfo.getSort() + " " + basePageInfo.getOrder();

        Object marketBrands = marketBrandMapper.selectMarketBrand(id, name, orderBy);

        return marketBrands;
    }

    @Override
    public AdminBrandCreateVo crate(MarketBrand marketBrand) {

        Date addTime = new Date();
        marketBrand.setAddTime(addTime);
        marketBrand.setUpdateTime(addTime);
        marketBrand.setDeleted(false);
        marketBrandMapper.insert(marketBrand);

        AdminBrandCreateVo adminBrandCreateVo = new AdminBrandCreateVo();
        BeanUtils.copyProperties(marketBrand, adminBrandCreateVo);

        return adminBrandCreateVo;
    }

    @Override
    public int update(AdminBrandCreateVo adminBrandCreateVo) {

        MarketBrand marketBrand = new MarketBrand();
        BeanUtils.copyProperties(adminBrandCreateVo, marketBrand);

        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();

        criteria.andIdEqualTo(marketBrand.getId());
        int affectRoes = marketBrandMapper.updateByExampleSelective(marketBrand, example);
        return affectRoes > 0 ? Constant.OK : Constant.NON;
    }

    @Override
    public void delete(Integer id) {
        marketBrandMapper.deleteMarketBrand(id);
    }

    // zxy,通过前台传来的品牌id查看品牌详情
    @Override
    public MarketBrand wxBrandDetail(Integer id) {
        MarketBrand marketBrand = marketBrandMapper.selectByPrimaryKey(id);
        return marketBrand;
    }

    // zxy,通过前台传来的page和limit查所有品牌列表
    @PageInfoAdvice
    @Override
    public Object wxBrandList(BasePageInfo basePageInfo) {
        // 查询条件
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.setDistinct(true);
        String orderBy = basePageInfo.getSort() + " " + basePageInfo.getOrder();
        marketBrandExample.setOrderByClause(orderBy);
        marketBrandExample.createCriteria().andDeletedEqualTo(false);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        // System.out.println(marketBrands.size());//输出测试查询结果条数，等于传入的limit,此处查询结果为10条
        return marketBrands;//返回第一次查询结果，在controller层转换
    }
    // zxy,通过前台传来的page和limit查所有品牌列表
    @Override
    public BasePageInfoVo<MarketBrandListVO> wxBrandListLarge(BasePageInfo basePageInfo) {
        PageHelper.startPage(basePageInfo.getPage(),basePageInfo.getLimit());
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.createCriteria().andDeletedEqualTo(false);
        marketBrandExample.setOrderByClause(basePageInfo.getSort()+" "+basePageInfo.getOrder());
        List<MarketBrand> marketBrandList = marketBrandMapper.selectByExample(marketBrandExample);
        // PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>();//!!!! error;查询结果需要作为形参传入;
        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>(marketBrandList);
        long total = marketBrandPageInfo.getTotal();
        int pages = marketBrandPageInfo.getPages();
        // 品牌所有列被查出，返回部分列，marketbrand--》marketbrandlistvo
        ArrayList<MarketBrandListVO> marketBrandListVOS = new ArrayList<>();
        for (MarketBrand marketBrand : marketBrandList) {
            MarketBrandListVO marketBrandListVO = new MarketBrandListVO();
            BeanUtils.copyProperties(marketBrand,marketBrandListVO);
            marketBrandListVOS.add(marketBrandListVO);
        }
        BasePageInfoVo<MarketBrandListVO> marketBrandListVOBasePageInfoVo = new BasePageInfoVo<>(total, pages,
                basePageInfo.getLimit(), basePageInfo.getPage(), marketBrandListVOS);
        return marketBrandListVOBasePageInfoVo;
    }
}
